package com.hzw.code.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import java.net.UnknownHostException;

/**
 * 公共工具类
 */
@Slf4j
public class CommonUtils {
    /**
     * 打印启动成功信息
     * @param application
     * @throws UnknownHostException
     */
    public static void printSysInfo(ConfigurableApplicationContext application) {
        Environment env = application.getEnvironment();
        String ip = SystemUtils.getLocalIp();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        if("/".equals(path)){
            path = "";
        }
        String projectName = env.getProperty("spring.application.name");
        log.info("\n------------------------------------------------------------------------------\n\t" +
                "Application " + projectName + " is running! Access URLs:\n\t" +
                "Local: \t\t\thttp://localhost:" + port + path + "/\n\t" +
                "External: \t\thttp://" + ip + ":" + port + path + "/\n\t" +
                "swagger-ui: \t\thttp://" + ip + ":" + port + path + "/swagger-ui.html\n\t" +
                "Doc: \t\t\thttp://" + ip + ":" + port + path + "/v2/api-docs\n" +
                "------------------------------------------------------------------------------");
    }
}
